<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="发货日期" prop="deliveryDate">
      <el-input v-model="dataForm.deliveryDate" placeholder="发货日期"></el-input>
    </el-form-item>
    <el-form-item label="业务员" prop="salesman">
      <el-input v-model="dataForm.salesman" placeholder="业务员"></el-input>
    </el-form-item>
    <el-form-item label="客户全名" prop="customerName">
      <el-input v-model="dataForm.customerName" placeholder="客户全名"></el-input>
    </el-form-item>
    <el-form-item label="单据类型代码" prop="documentTypeCode">
      <el-input v-model="dataForm.documentTypeCode" placeholder="单据类型代码"></el-input>
    </el-form-item>
    <el-form-item label="产品代号" prop="productCode">
      <el-input v-model="dataForm.productCode" placeholder="产品代号"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="发货数量" prop="deliveryQuantity">
      <el-input v-model="dataForm.deliveryQuantity" placeholder="发货数量"></el-input>
    </el-form-item>
    <el-form-item label="本币含税单价" prop="localCurrencyPrice">
      <el-input v-model="dataForm.localCurrencyPrice" placeholder="本币含税单价"></el-input>
    </el-form-item>
    <el-form-item label="本币含税金额" prop="localCurrencyAmount">
      <el-input v-model="dataForm.localCurrencyAmount" placeholder="本币含税金额"></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="region">
      <el-input v-model="dataForm.region" placeholder="所属区域"></el-input>
    </el-form-item>
    <el-form-item label="发货类型代码" prop="deliveryTypeCode">
      <el-input v-model="dataForm.deliveryTypeCode" placeholder="发货类型代码"></el-input>
    </el-form-item>
    <el-form-item label="发票号码" prop="invoiceNo">
      <el-input v-model="dataForm.invoiceNo" placeholder="发票号码"></el-input>
    </el-form-item>
    <el-form-item label="开票日期" prop="invoiceDate">
      <el-input v-model="dataForm.invoiceDate" placeholder="开票日期"></el-input>
    </el-form-item>
    <el-form-item label="发送时间" prop="sendTime">
      <el-input v-model="dataForm.sendTime" placeholder="发送时间"></el-input>
    </el-form-item>
    <el-form-item label="免税否" prop="isTaxExempt">
      <el-input v-model="dataForm.isTaxExempt" placeholder="免税否"></el-input>
    </el-form-item>
    <el-form-item label="发票类型代码" prop="invoiceTaxTypeCode">
      <el-input v-model="dataForm.invoiceTaxTypeCode" placeholder="发票类型代码"></el-input>
    </el-form-item>
    <el-form-item label="开票状态代码" prop="invoiceStatusCode">
      <el-input v-model="dataForm.invoiceStatusCode" placeholder="开票状态代码"></el-input>
    </el-form-item>
    <el-form-item label="不需要开票" prop="isNeedInvoice">
      <el-input v-model="dataForm.isNeedInvoice" placeholder="不需要开票"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="录入人" prop="createdBy">
      <el-input v-model="dataForm.createdBy" placeholder="录入人"></el-input>
    </el-form-item>
    <el-form-item label="录入时间" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder="录入时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deliveryDate: '',
          salesman: '',
          customerName: '',
          documentTypeCode: '',
          productCode: '',
          productName: '',
          deliveryQuantity: '',
          localCurrencyPrice: '',
          localCurrencyAmount: '',
          region: '',
          deliveryTypeCode: '',
          invoiceNo: '',
          invoiceDate: '',
          sendTime: '',
          isTaxExempt: '',
          invoiceTaxTypeCode: '',
          invoiceStatusCode: '',
          isNeedInvoice: '',
          remark: '',
          createdBy: '',
          createdAt: ''
        },
        dataRule: {
          deliveryDate: [
            { required: true, message: '发货日期不能为空', trigger: 'blur' }
          ],
          salesman: [
            { required: true, message: '业务员不能为空', trigger: 'blur' }
          ],
          customerName: [
            { required: true, message: '客户全名不能为空', trigger: 'blur' }
          ],
          documentTypeCode: [
            { required: true, message: '单据类型代码不能为空', trigger: 'blur' }
          ],
          productCode: [
            { required: true, message: '产品代号不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          deliveryQuantity: [
            { required: true, message: '发货数量不能为空', trigger: 'blur' }
          ],
          localCurrencyPrice: [
            { required: true, message: '本币含税单价不能为空', trigger: 'blur' }
          ],
          localCurrencyAmount: [
            { required: true, message: '本币含税金额不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          deliveryTypeCode: [
            { required: true, message: '发货类型代码不能为空', trigger: 'blur' }
          ],
          invoiceNo: [
            { required: true, message: '发票号码不能为空', trigger: 'blur' }
          ],
          invoiceDate: [
            { required: true, message: '开票日期不能为空', trigger: 'blur' }
          ],
          sendTime: [
            { required: true, message: '发送时间不能为空', trigger: 'blur' }
          ],
          isTaxExempt: [
            { required: true, message: '免税否不能为空', trigger: 'blur' }
          ],
          invoiceTaxTypeCode: [
            { required: true, message: '发票类型代码不能为空', trigger: 'blur' }
          ],
          invoiceStatusCode: [
            { required: true, message: '开票状态代码不能为空', trigger: 'blur' }
          ],
          isNeedInvoice: [
            { required: true, message: '不需要开票不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          createdBy: [
            { required: true, message: '录入人不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '录入时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cw/salesinvoice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deliveryDate = data.salesInvoice.deliveryDate
                this.dataForm.salesman = data.salesInvoice.salesman
                this.dataForm.customerName = data.salesInvoice.customerName
                this.dataForm.documentTypeCode = data.salesInvoice.documentTypeCode
                this.dataForm.productCode = data.salesInvoice.productCode
                this.dataForm.productName = data.salesInvoice.productName
                this.dataForm.deliveryQuantity = data.salesInvoice.deliveryQuantity
                this.dataForm.localCurrencyPrice = data.salesInvoice.localCurrencyPrice
                this.dataForm.localCurrencyAmount = data.salesInvoice.localCurrencyAmount
                this.dataForm.region = data.salesInvoice.region
                this.dataForm.deliveryTypeCode = data.salesInvoice.deliveryTypeCode
                this.dataForm.invoiceNo = data.salesInvoice.invoiceNo
                this.dataForm.invoiceDate = data.salesInvoice.invoiceDate
                this.dataForm.sendTime = data.salesInvoice.sendTime
                this.dataForm.isTaxExempt = data.salesInvoice.isTaxExempt
                this.dataForm.invoiceTaxTypeCode = data.salesInvoice.invoiceTaxTypeCode
                this.dataForm.invoiceStatusCode = data.salesInvoice.invoiceStatusCode
                this.dataForm.isNeedInvoice = data.salesInvoice.isNeedInvoice
                this.dataForm.remark = data.salesInvoice.remark
                this.dataForm.createdBy = data.salesInvoice.createdBy
                this.dataForm.createdAt = data.salesInvoice.createdAt
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cw/salesinvoice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deliveryDate': this.dataForm.deliveryDate,
                'salesman': this.dataForm.salesman,
                'customerName': this.dataForm.customerName,
                'documentTypeCode': this.dataForm.documentTypeCode,
                'productCode': this.dataForm.productCode,
                'productName': this.dataForm.productName,
                'deliveryQuantity': this.dataForm.deliveryQuantity,
                'localCurrencyPrice': this.dataForm.localCurrencyPrice,
                'localCurrencyAmount': this.dataForm.localCurrencyAmount,
                'region': this.dataForm.region,
                'deliveryTypeCode': this.dataForm.deliveryTypeCode,
                'invoiceNo': this.dataForm.invoiceNo,
                'invoiceDate': this.dataForm.invoiceDate,
                'sendTime': this.dataForm.sendTime,
                'isTaxExempt': this.dataForm.isTaxExempt,
                'invoiceTaxTypeCode': this.dataForm.invoiceTaxTypeCode,
                'invoiceStatusCode': this.dataForm.invoiceStatusCode,
                'isNeedInvoice': this.dataForm.isNeedInvoice,
                'remark': this.dataForm.remark,
                'createdBy': this.dataForm.createdBy,
                'createdAt': this.dataForm.createdAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
